package org.example.BinaryTree;

import java.util.ArrayList;
import java.util.List;

public class Binary_Tree_Postorder_Traversal {
    public static void main(String[] args) {
        //二叉树的后序遍历

        //给你一棵二叉树的根节点 root ，返回其节点值的 后序遍历 。
        TreeNode node1 = new TreeNode(5);
        TreeNode node2 = new TreeNode(4);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(2);
        TreeNode node5 = new TreeNode(1);
        TreeNode root = new TreeNode(0);
        root.left = node1;
        root.right = node2;
        node1.left = node3;
        node1.right =  node4;
        node2.left = node5;
        //root = null;
        for (Integer i : preorderTraversal1(root)) {
            System.out.print(i+" ");
        }
    }
    public static List<Integer> preorderTraversal1(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null) return list;
        get(root,list);
        return list;
    }
    public static void get(TreeNode root,List<Integer> list){
        if (root.left != null) get(root.left,list);
        if (root.right != null) get(root.right,list);
        list.add(root.val);
    }
}
